<?php

namespace app\modules\client\controllers;

use Yii;
use yii\web\Controller;
use app\libraries\Common;
use app\libraries\Curl;

class RequestController extends Controller{

    public $enableCsrfValidation = false;

    public function actionIndex(){

        $db = Yii::$app->db;
        $requests = $db->createCommand("SELECT `id`, `cid`, `name` ,`domain`, `uri`, `method` FROM `pre_request`")->queryAll();

        $data = [
            'requests' => $requests,
        ];

        //print_r($data);exit;

        return $this->renderPartial("index", $data);
    }

    public function actionList(){

        $db = Yii::$app->db;
        $requests = $db->createCommand("SELECT `id`, `cid`, `name` ,`domain`, `uri`, `method` FROM `pre_request`")->queryAll();

        $data = [
            'requests' => $requests,
        ];

        //print_r($data);exit;

        return $this->renderPartial("index", $data);
    }

    public function actionDetail(){

        $id = isset($_GET["id"]) ? addslashes($_GET["id"]) : 0;

        $request = \app\modules\client\models\Request::getRequest($id);

        $data = [
            'request' => $request,
        ];

        return $this->renderPartial('detail', $data);
    }

    /**
     * formdata
     */
    public function actionPost(){
        $id = isset($_GET["id"]) ? addslashes($_GET["id"]) : 0;
        $request = \app\modules\client\models\Request::getRequest($id);

        //Common::pre($request);

        $data = [];
        if(isset($request["params"]) && is_array($request["params"])) {
            foreach ($request["params"] as $param) {
                $data[$param["name"]] = isset($_POST[$param["name"]]) ? $_POST[$param["name"]] : $param["default"];
            }
        }

        echo "formdata:"."<br>";
        Common::pre($data);

        $request["domain"] = empty($request["domain"]) ? 'http://'.$_SERVER["HTTP_HOST"] : $request["domain"];

        $request_url = $request["domain"].$request["uri"];

        echo "请求地址：".$request_url."<br>";

        echo "---------------------------<br>";

        $r = Curl::post_formdata($request_url, $data);

        var_dump($r);

        if($r2 = json_decode($r)){
            Common::pre($r2);
        }


    }

    /**
     * formdata
     */
    public function actionPostRaw(){
        $id = isset($_GET["id"]) ? addslashes($_GET["id"]) : 0;
        $request = \app\modules\client\models\Request::getRequest($id);

        //Common::pre($request);

        $data = [];
        if(isset($request["params"]) && is_array($request["params"])) {
            foreach ($request["params"] as $param) {
                $data[$param["name"]] = isset($_POST[$param["name"]]) ? $_POST[$param["name"]] : $param["default"];
            }
        }

        echo "formdata:"."<br>";
        Common::pre($data);

        $request["domain"] = empty($request["domain"]) ? 'http://'.$_SERVER["HTTP_HOST"] : $request["domain"];

        $request_url = $request["domain"].$request["uri"];

        echo "请求地址：".$request_url."<br>";

        echo "---------------------------<br>";

        $raw = json_encode($data, JSON_UNESCAPED_UNICODE);

        $r = Curl::post_raw($request_url, $raw);

        var_dump($r);

        if($r2 = json_decode($r)){
            Common::pre($r2);
        }


    }


}
